package com.oba.king.businessprocess.storagemanage.instorage;

/**
 * Title:           InStorageBean
 * Description:
 * Copyright:       Copyright (c) 2010
 * Company:         King Top Limited
 * @author          Wu Liang
 * @version 1.0
 *
 * MODIFICATION HISTORY
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Date Modified        Modified By         Comments
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 28/04/2010           Wu Liang            Code Creation
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.AbortProcessingException;
import javax.faces.model.SelectItem;

import com.icesoft.faces.component.paneltabset.TabChangeEvent;
import com.oba.base.dao.DBDataModel;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.interfaces.PageCallBack;
import com.oba.base.service.BaseBean;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.Dept;

public class InStorageBean extends BaseBean<InStorage, InStorageService> {

	// 用于接收数据的List,用于在页面传递的list
	private DBDataModel inStorageList = DBDataModel.create(new PageCallBack() {
		public void afterLoadPage(List curPage) {
			for (Object obj : curPage) {
				InStorage spd = (InStorage) obj;
				spd.setInStoragePrdt(getService().getDao().findInStoragePrdtByInStoId(spd));
			}
		}
	});
	
	// 用于显示单独显示入库单中的详细产品
	private DBDataModel inStoragePrdtDetailData = DBDataModel.create(new PageCallBack() {
		public void afterLoadPage(List curPage) {
			for (Object obj : curPage) {
				InStoragePrdt spd = (InStoragePrdt) obj;
				spd.setProductInfo(getService().getDao().srchPrdtById(spd));
				spd.setInStorage(getService().getDao().srchInStorageById(spd));
			}
		}
	});
	
	//用于审核显示
	private DBDataModel inStorageAuditData = DBDataModel.create(new PageCallBack() {
		public void afterLoadPage(List curPage) {
			for (Object obj : curPage) {
				InStorage spd = (InStorage) obj;
				spd.setInStoragePrdt(getService().getDao().findInStoragePrdtByInStoId(spd));
			}
		}
	});
	
	//用于审核显示
	private DBDataModel inStorageAuditDetailData = DBDataModel.create(new PageCallBack() {
		public void afterLoadPage(List curPage) {
			for (Object obj : curPage) {
				InStoragePrdt spd = (InStoragePrdt) obj;
				spd.setProductInfo(getService().getDao().srchPrdtById(spd));
				spd.setInStorage(getService().getDao().srchInStorageById(spd));
			}
		}
	});

	// 用于传递页面之间的对象
	private InStorage inStorage;
	private InStoragePrdt inStoragePrdt;
	private ProductInfo productInfo;
	private final String ADD_NEW_INSTORAGE_PATH = "/business/inStorageAdd.jspx";
	private final String SHOW_INSTORAGE_PATH = "/business/inStorageShow.jspx";
	private final String SHOW_AUDITING_INSTORAGE_PATH = "/business/inStorageAuditing.jspx";
	private final String SHOW_DETAIL_INSTORAGE_PATH = "/business/inStorageDetailShow.jspx";
	private final long STATUS_NEW = 1;
	private final long STATUS_PASSED = 2;
	private final long STATUS_REJECT = 3;
	private final long STATUS_MODIFY_NO_AUDIT = 4;
	private InStorageCondition inStorageCondition;
	private int focusIndex;
//	private ArrayList inStoragePrdtList;
	// 保存从部门表中查询出来的部门信息
	private List<SelectItem> queryDepartList;

	// 用于保存一个入库单中的产品list
	private DBDataModel inStoragePrdtData = DBDataModel.create();
	
	// 所有产品列表
	private DBDataModel choosePrdtList = DBDataModel.create();

	// 从入库单显示页面到详细显示页面的入库单号，用于查询入库单中的详细信息
	private String inStorageCode;
	private boolean showPrdtForInStorage = false;

	//用于判断录入、审核和查询
	private boolean audit = false;
	/**
	 * Tabbed placement, possible values are "top" and "bottom", the default is
	 * "bottom".
	 */
	private String tabPlacement = "top";

	/**
	 * 查询所有，初始化时显示
	 */
	@Override
	public String doFind() {
		inStorageList.find(getService().getDao().buildAll());
		inStorageAuditData = inStorageList;
		return null;
	}

	@Override
	public void init() {
		doFind();
		inStorage = new InStorage();
		inStoragePrdt = new InStoragePrdt();
		productInfo = new ProductInfo();
		inStorageCondition = new InStorageCondition();
		queryDepartList = new ArrayList<SelectItem>();
		queryDepartList.add(new SelectItem(null, "全部"));
		for (Dept dept : getService().getDao().buildAllDept()) {
			queryDepartList.add(new SelectItem(dept, dept.getName()));
		}
		inStoragePrdtData.setWrappedData(new ArrayList<InStoragePrdt>());
		audit = false;
	}
	
	//点击入库单录入节点进入页面并初始化
	public void initAddInStorage(){
		inStorage = new InStorage();
		inStorage.setNewBo(true);
		inStoragePrdt = new InStoragePrdt();
		productInfo = new ProductInfo();
		inStorageCondition = new InStorageCondition();
		queryDepartList = new ArrayList<SelectItem>();
		queryDepartList.add(new SelectItem(null, "全部"));
		for (Dept dept : getService().getDao().buildAllDept()) {
			queryDepartList.add(new SelectItem(dept, dept.getName()));
		}
		focusIndex = 0;
		goTarget(ADD_NEW_INSTORAGE_PATH);
	}
	
	//点击入库单审核时初始化
	public void initInStorageAudit(){
		System.out.println("Start initInStorageAudit>>>>");
		inStorageCondition = new InStorageCondition();
		inStorageList.find(getService().getDao().findNeedAuditInStorage());
		audit = true;
	}
	

	/**
	 * 新增入库单
	 */
	public void addNewInStorageInfo() {
		focusIndex = 0;
		inStorage = new InStorage();
		this.inStorage.setNewBo(true);
		this.goTarget(this.ADD_NEW_INSTORAGE_PATH);
	}

	/**
	 * 在显示页面(inStorageShow)删除入库单,同时也就删除了入库产品中间表中的数据
	 */
	public void doDelInStorageInfo() {
		// System.out.println("Start InStorageBean doDelInStorageInfo()>>>>");
		if (this.inStorageList.isRowAvailable()) {
			this.getService().getDao().deleteInStorePrdt((InStorage) inStorageList.getRowData());
			this.goTarget(this.SHOW_INSTORAGE_PATH);
			this.doFind();
		}
		// System.out.println("End InStorageBean doDelInStorageInfo()<<<<");
	}

	/**
	 * 修改入库单
	 */
	public void doEditInStorageInfo() {
		if (this.inStorageList.isRowAvailable()) {
			focusIndex = 0;
			this.inStorage = (InStorage) this.inStorageList.getRowData();
			List<InStoragePrdt> list = this.getService().getDao().srchInStoragePrdt(inStorage);
			inStoragePrdtData.setWrappedData(list);
			this.goTarget(ADD_NEW_INSTORAGE_PATH);
		}
	}

	/**
	 * 保存入库单信息
	 */
	public void doSaveInStorageInfo() {
		String isp = new SimpleDateFormat("yyyyMMdd").format(new Date()) + inStorage.getBelegCode();
		System.out.println("Start InStorageBean doSaveInStorageInfo()>>>>");
		inStorage.setCreateDate(new Date());
		inStorage.setInDate(new Date());
		inStorage.setInStorageCode(isp);
		inStorage.setUserInfo(getSysUser());
		if(inStorage.isNewBo()||"null".equals(inStorage.getAuditStatus().toString())){
			inStorage.setAuditStatus(STATUS_NEW);
		}else if("3".equals(inStorage.getAuditStatus().toString())){
			inStorage.setAuditStatus(STATUS_MODIFY_NO_AUDIT);
		}
		this.getService().save(this.inStorage);
		List<InStoragePrdt> list = new ArrayList<InStoragePrdt>();
		list = this.getService().getDao().srchInStoragePrdt(inStorage);
		for (InStoragePrdt i : list) {
			i.setChecked(true);
		}
		inStoragePrdtData.setWrappedData(list);

		focusIndex = 1;
		System.out.println("End InStorageBean doSaveInStorageInfo()<<<<");
	}

	/**
	 * 根据inStorageCondition进行模糊查询入库单
	 */
	public void srchInStoragCondition() {
		inStorageList.find(this.getService().getDao().srchByInStorageCondition(inStorageCondition));
	}

	/**
	 * 跳转到另一个页面用于显示入库单中详细信息 用inStoragePrdtDetailData来存
	 */
	public void doShowDetails() {
		if (this.inStorageList.isRowAvailable()) {
			inStorage = (InStorage)inStorageList.getRowData();
			inStoragePrdtDetailData.find(this.getService().getDao().srchByInStorageCode((InStorage) inStorageList.getRowData()));
			goTarget(SHOW_DETAIL_INSTORAGE_PATH);
		}
	}

	/**
	 *  用于跳转到入库单详细进行审核
	 */
	public void doShowAuditingDetails(){
		if (inStorageAuditData.isRowAvailable()) {
			inStorageAuditDetailData.find(this.getService().getDao().srchByInStorageCode((InStorage) inStorageAuditData.getRowData()));
			focusIndex = 0;
			this.inStorage = (InStorage) this.inStorageList.getRowData();
			List<InStoragePrdt> list = this.getService().getDao().srchInStoragePrdt(inStorage);
			inStoragePrdtData.setWrappedData(list);
			goTarget(SHOW_AUDITING_INSTORAGE_PATH);
		}
	}	
	
	public void doAudit(){
		if(inStorageAuditData.isRowAvailable()){
			inStorageAuditDetailData.find(this.getService().getDao().srchByInStorageCode((InStorage) inStorageAuditData.getRowData()));
			inStorage = (InStorage) this.inStorageList.getRowData();
			if(inStorageAuditDetailData.getAllData().size()>0){
				goTarget(SHOW_AUDITING_INSTORAGE_PATH);
			}
		}
	}
	
	/**
	 * 用于审核不通过
	 */
	public void auditReject(){
		inStorage.setAuditStatus(STATUS_REJECT);
		getService().getDao().save(inStorage);
		inStorageList.find(getService().getDao().findNeedAuditInStorage());
		goTarget(SHOW_INSTORAGE_PATH);
	}
	
	/**
	 * 用于审核通过
	 */
	public void auditPass(){
		inStorage.setAuditStatus(STATUS_PASSED);
		getService().getDao().save(inStorage);
		inStorageList.find(getService().getDao().findNeedAuditInStorage());
		goTarget(SHOW_INSTORAGE_PATH);
	}

	/**
	 * 增加一个新的产品在入库产品中间表中
	 */
	public void addNewInStoragePrdt() {
		inStorageCondition = new InStorageCondition();
		List list = (List) inStoragePrdtData.getWrappedData();
		choosePrdtList.find(this.getService().getDao().srchPrdtForChoose((List<IBaseBo>) list));
		showPrdtForInStorage = true;
	}

	public void doChoosedPrdt() {
		// System.out.println(inStoragePrdtData.getWrappedData());
		// 选择这个药品
		if (choosePrdtList.isRowAvailable()) {
			ProductInfo prdtInfo = (ProductInfo) choosePrdtList.getRowData();
			List list = (List) inStoragePrdtData.getWrappedData();
			inStoragePrdt = new InStoragePrdt();
			inStoragePrdt.setInStorage(inStorage);
			inStoragePrdt.setProductInfo(prdtInfo);
			inStoragePrdt.setUnitPrice(prdtInfo.getProductUnitPrice());
			inStoragePrdt.setNewBo(true);
			inStoragePrdt.setProductInfo(prdtInfo);
			inStoragePrdt.setInStorage(inStorage);
			list.add(inStoragePrdt);
			showPrdtForInStorage = false;
		}
	}

	/**
	 * 修改入库产品中间表中的信息
	 */
	public void doEditInStoragePrdt() {
		if (inStoragePrdtData.isRowAvailable()) {
			System.out.println("Entered the doEditInStoragePrdt to modify the inStoragePrdt");
			InStoragePrdt isp = (InStoragePrdt) inStoragePrdtData.getRowData();
			isp.setChecked(false);
		}
	}

	/**
	 * 删除入库产品中间表中的信息
	 */
	public void doDelInStoragePrdt() {
		if (inStoragePrdtData.isRowAvailable()) {
			// save row
			InStoragePrdt isp = (InStoragePrdt) inStoragePrdtData.getRowData();
			List list = (List) inStoragePrdtData.getWrappedData();
			list.remove(isp);
			getService().del(isp);
		}
	}

	/**
	 * 保存入库产品中间表中的信息
	 */
	public void doSaveInStoragePrdt() {
		if (inStoragePrdtData.isRowAvailable()) {
			// save row
			InStoragePrdt isp = (InStoragePrdt) inStoragePrdtData.getRowData();
			// if(isp.getInStorage().getInStorageId()!=null&&"".equals(isp.getInStorage().getInStorageId())){
			NumberFormat format = new DecimalFormat("#0.00");
			isp.setTotalSum(Double.valueOf(format.format(isp.getUnitPrice() * isp.getInAmount())));
			isp.setChecked(true);
			System.out.println("isp.getInStorage().getInStorageId()" + isp.getInStorage().getInStorageId()
					+ "\nisp.getProductInfo().getProductId()" + isp.getProductInfo().getProductId());
			getService().save(isp);
			// }
		}
	}

	/**
	 *  TODO 查询指定药品
	 */
	@SuppressWarnings("unchecked")
	public void srchByPrdtIdAndName() {
		System.out.println("inStorageCondition.getProductSerialId() = "+inStorageCondition.getProductSerialId()+"inStorageCondition.getProductName() = "+inStorageCondition.getProductName());
		//List l = inStoragePrdtData.getAllData();
		List<InStoragePrdt> list = (List) inStoragePrdtData.getWrappedData();
		//System.out.println(l);
		List<Long> productIdList = new ArrayList<Long>() ;
		for(InStoragePrdt isp: list){
			productIdList.add(isp.getProductInfo().getProductId());
		}
		choosePrdtList.find(this.getService().getDao().srchPrdtForChoose(productIdList, inStorageCondition));
	}
	

	/**
	 * 清除页面查询的条件
	 */
	public void cleanButton() {
		this.inStorageCondition = new InStorageCondition();
		this.doFind();
	}
	
	public void cleanSrchPrdtButton() {
		this.inStorageCondition = new InStorageCondition();
		List list = (List) inStoragePrdtData.getWrappedData();
		choosePrdtList.find(this.getService().getDao().srchPrdtForChoose((List<IBaseBo>) list));
	}

	/**
	 * Called when the table binding's tab focus changes.
	 * 
	 * @param tabChangeEvent
	 *            used to set the tab focus.
	 * @throws AbortProcessingException
	 *             An exception that may be thrown by event listeners to
	 *             terminate the processing of the current event.
	 */
	public void processTabChange(TabChangeEvent tabChangeEvent) throws AbortProcessingException {
		// 如果没有新增入库单，则不允许新增入库产品中间表
		if (inStorage.getId() == null || "".equals(inStorage.getId())) {
			focusIndex = 0;
		}else{
			//如果入库单存在并有数据，则需要显示修改及保存
			List<InStoragePrdt> list = (List) inStoragePrdtData.getWrappedData();
			for (InStoragePrdt i : list) {
				i.setChecked(true);
			}
		}
	}
	
	/**
	 * 关闭选择药品
	 */
	public void closeShowAddSamplePrdt() {
		this.showPrdtForInStorage = false;
	}

	/**
	 * 修改或增加后的返回显示页面
	 */
	public void doRturnShow() {
		doFind();
		this.goTarget(this.SHOW_INSTORAGE_PATH);
	}
	
	public void doRturnAuditShow(){  
		inStorageList.find(getService().getDao().findNeedAuditInStorage());
		goTarget(SHOW_INSTORAGE_PATH);
	}

	public DBDataModel getInStorageList() {
		return inStorageList;
	}

	public void setInStorageList(DBDataModel inStorageList) {
		this.inStorageList = inStorageList;
	}

	public InStorage getInStorage() {
		return inStorage;
	}

	public void setInStorage(InStorage inStorage) {
		this.inStorage = inStorage;
	}

	public InStorageCondition getInStorageCondition() {
		return inStorageCondition;
	}

	public void setInStorageCondition(InStorageCondition inStorageCondition) {
		this.inStorageCondition = inStorageCondition;
	}

	public String getInStorageCode() {
		return inStorageCode;
	}

	public void setInStorageCode(String inStorageCode) {
		this.inStorageCode = inStorageCode;
	}

	public InStoragePrdt getInStoragePrdt() {
		return inStoragePrdt;
	}

	public void setInStoragePrdt(InStoragePrdt inStoragePrdt) {
		this.inStoragePrdt = inStoragePrdt;
	}

	public String getTabPlacement() {
		return tabPlacement;
	}

	public void setTabPlacement(String tabPlacement) {
		this.tabPlacement = tabPlacement;
	}

	public boolean isShowPrdtForInStorage() {
		return showPrdtForInStorage;
	}

	public void setShowPrdtForInStorage(boolean showPrdtForInStorage) {
		this.showPrdtForInStorage = showPrdtForInStorage;
	}

	public int getFocusIndex() {
		return focusIndex;
	}

	public void setFocusIndex(int focusIndex) {
		this.focusIndex = focusIndex;
	}

	public DBDataModel getInStoragePrdtData() {
		return inStoragePrdtData;
	}

	public void setInStoragePrdtData(DBDataModel inStoragePrdtData) {
		this.inStoragePrdtData = inStoragePrdtData;
	}

	public List<SelectItem> getQueryDepartList() {
		return queryDepartList;
	}

	public void setQueryDepartList(List<SelectItem> queryDepartList) {
		this.queryDepartList = queryDepartList;
	}

	public DBDataModel getChoosePrdtList() {
		return choosePrdtList;
	}

	public void setChoosePrdtList(DBDataModel choosePrdtList) {
		this.choosePrdtList = choosePrdtList;
	}

	public DBDataModel getInStoragePrdtDetailData() {
		return inStoragePrdtDetailData;
	}

	public void setInStoragePrdtDetailData(DBDataModel inStoragePrdtDetailData) {
		this.inStoragePrdtDetailData = inStoragePrdtDetailData;
	}

	public DBDataModel getInStorageAuditData() {
		return inStorageAuditData;
	}

	public void setInStorageAuditData(DBDataModel inStorageAuditData) {
		this.inStorageAuditData = inStorageAuditData;
	}

	public DBDataModel getInStorageAuditDetailData() {
		return inStorageAuditDetailData;
	}

	public void setInStorageAuditDetailData(DBDataModel inStorageAuditDetailData) {
		this.inStorageAuditDetailData = inStorageAuditDetailData;
	}

	public boolean isAudit() {
		return audit;
	}

	public void setAudit(boolean audit) {
		this.audit = audit;
	}

}
